/*************************************************
Author: tony
Date: 02/26/2013
Description:
Notes:
*************************************************/

#ifndef __CGfitConfig_h__
#define __CGfitConfig_h__

#include "CDropItemSubType.h"

//单个礼包中最多可配置的物品数
#define MAX_GFIT_CONFIG_ITEMS    10

class CGiftItem
{
public:
    CGiftItem() : _ucItemType(0), _usItemId(0), _uiItemCount(0)
    {

    }
    unsigned char   _ucItemType;
    unsigned short  _usItemId;
    unsigned int    _uiItemCount;
};

class CGiftDropItemConfig
{
public:
    CGiftDropItemConfig()
    {
        _iDropRate = 0;
        _szItemSubType.Set("%s","");
    }

public:
    CGiftItem _stItem;
    int _iDropRate;
    CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY  _szItemSubType;
};

class CGiftConfigUnit
{
public:
	CGiftConfigUnit()  :_bHasConfig(false)
    {
        _usGiftId = 0;
        _ucGiftType = 0;
        _usMutexType = 0;
        _shRequiredLevel = 0;
        _iDropRateBase = 0;
        _iBuySilverPrice = 0;
        _iBuyGoldPrice = 0;
        _iBuyContPrice = 0;
        _iSellSilverPrice = 0;
        _bDirectOpen = true;
        _astToDropItems.Clear();
        _uiTipsType = 0;
	}

public:
    bool        _bHasConfig;
    unsigned short  _usGiftId;     //礼包ID
    unsigned char  _ucGiftType;    //1.  每种掉落物品单独按照概率进行掉落    2.将所有掉落物品的掉率加总进行roll点  3.礼包中配置的每种物品必然掉落
    unsigned short _usMutexType;   //礼包互斥类型
    short _shRequiredLevel;        //使用需要的主角等级
    int _iDropRateBase;            //掉落概率基数
    bool _bDirectOpen;             //直接打开为true,放入背包中为false
    CTSizeString<MAX_ICON_NAME_LEN> _szIconName;
    int _iBuyContPrice;
    int _iBuyGoldPrice;            //商店买入的银币价
    int _iBuySilverPrice;          //商店买入的银币价
    int _iSellSilverPrice;         //卖店的银币价

    int            _iDropBeginTime;  //掉落开始时间
    int            _iDropEndTime;    //掉落结束时间
    unsigned int  _uiTipsType;      //使用后的跑马灯

    int AddDropItemConfig (CGiftDropItemConfig & stConfig)
    {
        if (_astToDropItems.Size() >= MAX_GFIT_CONFIG_ITEMS)
        {
            return ERROR_GIFT_CONFIG_INVALID;
        }

        if (stConfig._stItem._ucItemType == 0)
        {
            return 0;
        }

        _astToDropItems.AddOneItem(stConfig);
        return 0;
    }

    CTArray<CGiftDropItemConfig, MAX_GFIT_CONFIG_ITEMS> _astToDropItems;
};

enum EN_GIFT_TYPE
{
    GIFT_TYPE_MIN      = 1,
    GIFT_TYPE_SEPERATE = 1,
    GIFT_TYPE_TOTAL    = 2,
    GIFT_TYPE_FIXED    = 3,
    GIFT_TYPE_MAX      = 3,
};

//最多的不同礼包数
#define MAX_GFIT_CONFIG_ID    1024
class CGiftConfig
{
public:
    typedef CTArray<CGiftItem, MAX_GFIT_CONFIG_ITEMS> T_GIFT_ITEM_TYPE;

	CGiftConfig()
    {
        _astGifts.m_iUsedCount = MAX_GFIT_CONFIG_ID + 1;
	}

    int AddConfig(CGiftConfigUnit& stConfigData)
    {
        if (stConfigData._usGiftId > MAX_GFIT_CONFIG_ID)
        {
            CT_ERROR(("add gift config error, id %d", stConfigData._usGiftId));
            return ERROR_GIFT_CONFIG_INVALID;
        }

        /* //由于存在用来发放奖励的礼包，所以不做检查
        if (stConfigData._ucGiftType > GIFT_TYPE_MAX
            || stConfigData._ucGiftType < GIFT_TYPE_MIN)
        {
            CT_ERROR(("add gift config error, id %d", stConfigData._ucGiftType));
            return ERROR_GIFT_CONFIG_INVALID;
        }
        */

        if (_astGifts[stConfigData._usGiftId]._bHasConfig)
        {
            CT_ERROR(("add duplicated gift config error, id %d", stConfigData._usGiftId));
            return ERROR_GIFT_CONFIG_INVALID;
        }

        _astGifts[stConfigData._usGiftId] = stConfigData;
        _astGifts[stConfigData._usGiftId]._bHasConfig = true;

        return 0;
    }

    bool IsGiftValid(unsigned int uiGiftId)
    {
        if (0 == uiGiftId
            || uiGiftId > MAX_GFIT_CONFIG_ID)
        {
            CT_ERROR(("gift config error, id %d", uiGiftId));
            return false;
        }

        if (!_astGifts[uiGiftId]._bHasConfig)
        {
            CT_ERROR(("gift config error, id %d", uiGiftId));
            return false;
        }

        return true;
    }

    int GetGiftConfig(unsigned int uiGfitID, CGiftConfigUnit& stConfigData)
    {
        if (!IsGiftValid(uiGfitID))
        {
            return ERROR_GIFT_CONFIG_INVALID;
        }

        stConfigData = _astGifts[uiGfitID];

        return 0;
    }

public:
    CTArray<CGiftConfigUnit, MAX_GFIT_CONFIG_ID+1> _astGifts;
};


#endif // __CGfitConfig_h__

